

<!--
All rights Reserved, Designed By www.youyacao.com
@Description:用户列表
@author:成都市一颗优雅草科技有限公司
@version V4.2
注意：本前端源码遵循 MIT开源协议本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:422108995 23625059584
开源gitee地址：https://gitee.com/youyacao/songshu-video-page
 -->

<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 用户列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
        >批量删除
        </el-button>
        <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select>-->
        <el-input v-model="query.name" placeholder="用户名/用户ID/手机号" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="用户名"></el-table-column>
        <el-table-column label="用户名ID">
          <template slot-scope="scope">{{scope.row.custom_id==null?'未设置':scope.row.custom_id}}</template>
        </el-table-column>
        <el-table-column prop="money" align="center"  label="账户余额"></el-table-column>
        <el-table-column label="VIP/到期时间">
          <template slot-scope="scope">{{scope.row.vip_end!=null?scope.row.vip_end:"未开通"}}</template>
        </el-table-column>
        <el-table-column label="头像(查看大图)" align="center">
          <template slot-scope="scope">
            <el-image
              class="table-td-thumb"
              :src="$utils.formatUrl(scope.row.head_img)"
              :preview-src-list="[$utils.formatUrl(scope.row.head_img)]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="邀请码" prop="invite_code" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.invit_code">{{scope.row.invit_code}}</div>
            <el-button size="mini" @click="createInvite(scope.row)" v-else>立即生成</el-button>
          </template>
        </el-table-column>
        <el-table-column label="推荐人" prop="parent_id" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.parent_id">
              <div>{{scope.row.parent_name}}</div>
              <div>邀请码：{{scope.row.reg_code}}</div>
            </div>
            <div v-else style="color: #8c939d;">无</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">

            <el-tag class="pointer" v-if="scope.row.disable==0" type="success"
                    @click="handleState(scope.$index, scope.row)">正常
            </el-tag>
            <el-tag class="pointer" v-if="scope.row.disable==1" type="danger"
                    @click="handleState(scope.$index, scope.row)">禁用
            </el-tag>
            <div v-if="scope.row.disable==1">
              截止：{{scope.row.disable_time}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="注册时间"></el-table-column>
        <!-- <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.state==='成功'?'success':(scope.row.state==='失败'?'danger':'')"
            >{{scope.row.state}}</el-tag>
          </template>
        </el-table-column>-->
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input v-model="form.custom_id"></el-input>
        </el-form-item>

        <el-form-item label="VIP到期时间" prop="vip_end">
          <el-date-picker
            type="date"
            placeholder="选择到期时间"
            v-model="form.vip_end"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="头像">
          <el-image v-show="form.head_img" class="type-icon" :preview-src-list="[$utils.formatUrl(form.head_img)]"
                    :src="$utils.formatUrl(form.head_img)"></el-image>
          <el-upload
            class="upload-demo"
            :limit="1"
            :show-file-list="false"
            :data="{type:'img'}"
            :action="$store.state.frontUrl+'/api/api/upload'"
            :file-list="fileList"
            list-type="picture"
            :on-success="uploadSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <el-input type="hidden" v-model="form.head_img"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'basetable',
    data() {
      return {
        fileList: [],
        query: {
          name: '',
          page: 1,
          pageSize: 10
        },
        tableData: [],
        multipleSelection: [],
        delList: [],
        editVisible: false,
        total: 0,
        form: {},
        idx: -1,
        id: -1
      }
    },
    created() {
      this.getData()
    },
    methods: {

      // 获取 easy-mock 的模拟数据
      getData() {
        this.$api.user.list(this.query).then(res => {
          this.tableData = res.data
          this.total = res.total || 0
        })
      },
      // 触发搜索按钮
      handleSearch() {
        this.$set(this.query, 'page', 1)
        this.getData()
      },
      // 用户状态更改
      handleState(index, row) {
        console.log("disable:",row.disable);
        if (row.disable == 0) {
          this.$prompt('请输入封禁到期时间', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /^(\d{4})(-)(\d{2})(-)(\d{2})$/,
            inputErrorMessage: '日期格式不正确',
            inputType:'date'
          }).then(({value}) => {
            var temp_row = row;
            temp_row.disable = 1;
            temp_row.disable_time = value;
            this.$api.user.update(temp_row).then(res => {
              if (res.code == 1) {
                this.$message.error(`修改失败,` + res.msg)
                return
              }
              this.$message.success(`修改成功`)
              this.getData()
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '取消输入'
            });
          });
        } else
        {
          this.$confirm('是否取消禁用?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
            var temp_row = row;
            temp_row.disable = 0;
            temp_row.disable_time = null;
            this.$api.user.update(temp_row).then(res => {
              if (res.code == 1) {
                this.$message.error(`修改失败,` + res.msg)
                return
              }
              this.$message.success(`修改成功`)
              this.getData()
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '修改失败'
            });
          });
        }

      },
      // 删除操作
      handleDelete(index, row) {
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        })
          .then(() => {

            var arr = [row.id]
            this.$api.user.delete({ids: arr}).then(res => {
              if (res.code == 1) {
                this.$message.error('删除失败,' + res.msg)
                return
              }
              this.getData()
              this.$message.success('删除成功')
            })
          })
          .catch(() => {
          })
      },
      // 多选操作
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      delAllSelection() {
        const length = this.multipleSelection.length
        var arr = []
        this.delList = this.delList.concat(this.multipleSelection)
        for (let i = 0; i < length; i++) {
          arr.push(this.multipleSelection[i].id)
        }
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        })
          .then(() => {
            this.$api.user.delete({ids: arr}).then(res => {
              if (res.code == 1) {
                this.$message.error('删除失败,' + res.msg)
                return
              }
              this.getData()
              this.$message.success('删除成功')
            })
          })
          .catch(() => {
          })
        this.multipleSelection = []
      },
      createInvite(row){
        this.$api.user.invite({id:row.id}).then(res => {
          if (res.code == 1) {
            this.$message.error('生成失败,' + res.msg)
            return
          }
          this.getData()
          this.$message.success('生成成功')
        })
      },
      // 编辑操作
      handleEdit(index, row) {
        this.idx = index
        this.form = row
        this.editVisible = true
      },
      // 保存编辑
      saveEdit() {
        this.$api.user.update(this.form).then((res) => {
          if (res.code == 1) {
            this.$message.error(`修改失败,` + res.msg)
            return
          }
          this.editVisible = false
          this.$message.success(`修改成功`)
          this.$set(this.tableData, this.idx, this.form)
        })

      },
      // 分页导航
      handlePageChange(val) {
        this.$set(this.query, 'page', val)
        this.getData()
      },
      uploadSuccess(response, file, fileList) {
        if (response.code === 0) {
          this.form.head_img = response.data.url
          this.fileList = []
        } else {
          this.$message.error('上传失败,' + response.msg)
        }

      }
    }
  }
</script>

<style scoped>
  .pointer {
    cursor: pointer;
  }

  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .red {
    color: #ff0000;
  }

  .mr10 {
    margin-right: 10px;
  }

  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }

  .type-icon {
    display: block;
    width: 150px;
    height: 150px;
  }
</style>
